/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface;
import entidades.*;
import Controladores.*;
import Controladores.exceptions.PreexistingEntityException;
import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
/**
 *
 * @author Gatoper
 */
public class JPIOwner extends javax.swing.JPanel {

    /**
     * Creates new form JPIOwner
     */
    public JPIOwner() {
        initComponents();
        this.setSize(new Dimension(300, 400));
    }
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Tbirthplace = new javax.swing.JTextField();
        Tname = new javax.swing.JTextField();
        Llastname = new javax.swing.JLabel();
        CByear = new javax.swing.JComboBox();
        Lname = new javax.swing.JLabel();
        TSSN = new javax.swing.JTextField();
        Tlastname = new javax.swing.JTextField();
        LSSN = new javax.swing.JLabel();
        Lbirthplace = new javax.swing.JLabel();
        Lbirthday = new javax.swing.JLabel();
        CBmonth = new javax.swing.JComboBox();
        CBDay = new javax.swing.JComboBox();
        Bcreate = new javax.swing.JButton();

        Sletras(Tbirthplace);

        Sletras(Tname);
        Tname.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TnameActionPerformed(evt);
            }
        });

        Llastname.setText("Lastname :");

        CByear.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1991", "1992", "1993", "1994","1995", "1996", "1997", "1998" }));

        Lname.setText("Name :");

        Snumero(TSSN);
        TSSN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TSSNActionPerformed(evt);
            }
        });

        Sletras(Tlastname);

        LSSN.setText("SSN");

        Lbirthplace.setText("Birthplace :");

        Lbirthday.setText("Birthday :");

        CBmonth.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4","5", "6", "7", "8","9", "10", "11", "12" }));

        CBDay.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4","5", "6", "7", "8","9", "10", "11", "12","13", "14", "15", "16" }));

        Bcreate.setText("Create");
        Bcreate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BcreateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Lbirthplace)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(Tbirthplace))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Lbirthday)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(CBDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(CBmonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(CByear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(LSSN)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(TSSN))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Llastname)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(Tlastname))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Lname)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(Tname, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(79, 79, 79)
                        .addComponent(Bcreate)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Lname)
                    .addComponent(Tname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Llastname)
                    .addComponent(Tlastname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LSSN)
                    .addComponent(TSSN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Lbirthday)
                    .addComponent(CBDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CBmonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CByear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Lbirthplace)
                    .addComponent(Tbirthplace, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(Bcreate)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void TnameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TnameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TnameActionPerformed

    private void TSSNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TSSNActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TSSNActionPerformed

    private void BcreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BcreateActionPerformed
        // TODO add your handling code here:
        boolean flag=true;
        if(TSSN.getText().length()<1) flag=false;
        if(Tbirthplace.getText().length()<1) flag=false;
        if(Tlastname.getText().length()<1) flag=false;
        if(Tname.getText().length()<1) flag=false;
        if(flag){
            EntityManagerFactory emf;
            EntityManager em;
            Map<String, String> properties = new HashMap<>();
            emf = Persistence.createEntityManagerFactory("HPVAS");
            em = emf.createEntityManager();
           //Person Pers=new Person(PersonJpaController PersJpaC=new PersonJpaController(emf);

            Owner Own=new Owner(this.Tname.getText(),this.Tlastname.getText(),this.TSSN.getText(),this.CByear.getSelectedItem().toString()+"-"+this.CBmonth.getSelectedItem().toString()+"-"+this.CBDay.getSelectedItem().toString()+" 00:00:00",this.Tbirthplace.getText());
            OwnerJpaController OwnJpaC=new OwnerJpaController(emf);

            //Pers.setIdperson(Integer.parseInt(this.Tid.getText()));
            //Own.setPersonIdperson(Integer.parseInt(this.Tid.getText()));
            try {
             //   PersJpaC.create(Pers);
                OwnJpaC.create(Own);
                JOptionPane.showMessageDialog(this, "Exito");
            } catch (PreexistingEntityException ex) {
                Logger.getLogger(JPIOwner.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                Logger.getLogger(JPIOwner.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else{
            JOptionPane.showMessageDialog(this, "Por favor llene todos los campos");
        }
    }//GEN-LAST:event_BcreateActionPerformed
    public void Snumero(JTextField a){
    a.addKeyListener(new KeyAdapter() {
    public void keyTyped(KeyEvent e){
        char c=e.getKeyChar();
        if (Character.isLetter(c)) {
            e.consume();
        }
    }
    });
    }
    public void Sletras(JTextField a){
    a.addKeyListener(new KeyAdapter() {
    public void keyTyped(KeyEvent e){
        char c=e.getKeyChar();
        if (Character.isDigit(c)) {
            e.consume();
        }
    }
    });
    } 
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Bcreate;
    private javax.swing.JComboBox CBDay;
    private javax.swing.JComboBox CBmonth;
    private javax.swing.JComboBox CByear;
    private javax.swing.JLabel LSSN;
    private javax.swing.JLabel Lbirthday;
    private javax.swing.JLabel Lbirthplace;
    private javax.swing.JLabel Llastname;
    private javax.swing.JLabel Lname;
    private javax.swing.JTextField TSSN;
    private javax.swing.JTextField Tbirthplace;
    private javax.swing.JTextField Tlastname;
    private javax.swing.JTextField Tname;
    // End of variables declaration//GEN-END:variables
}
